package com.tanqidi.arcgisandroid.base.utils

import android.graphics.Color
import com.esri.arcgisruntime.arcgisservices.LabelDefinition
import com.esri.arcgisruntime.arcgisservices.LabelingPlacement
import com.esri.arcgisruntime.data.*
import com.esri.arcgisruntime.geometry.GeometryType
import com.esri.arcgisruntime.layers.FeatureLayer
import com.esri.arcgisruntime.mapping.labeling.ArcadeLabelExpression
import com.esri.arcgisruntime.mapping.labeling.LabelOverrunStrategy
import com.esri.arcgisruntime.symbology.*
import com.tanqidi.arcgisandroid.data.layerConfig.Layer

object SymbolUtils {

    private val textSymbol = TextSymbol().apply {
        size = 11f
        color = Color.BLUE
        outlineColor = Color.WHITE
        haloColor = Color.WHITE
        haloWidth = 2f
    }

    fun symbol(layer: Layer, featureLayer: FeatureLayer){
        //设置label标注字段和样式
//        val labelDefinition = LabelDefinition(ArcadeLabelExpression("\$feature.${layer.label} + ' 继续, ' + \$feature.${layer.label}"), textSymbol).apply {
        val labelDefinition = LabelDefinition(ArcadeLabelExpression("\$feature.${layer.label}"), textSymbol).apply {
            isUseCodedValues = true
            overrunStrategy = LabelOverrunStrategy.ALLOW
            offsetDistance = layer.width.toDouble() * 0.3
        }
        //开启标注和添加标注表达式
        featureLayer.isLabelsEnabled = true
        featureLayer.labelDefinitions.add(labelDefinition)

        var simpleRenderer: SimpleRenderer? = null
        when(featureLayer.featureTable.geometryType){
            GeometryType.POINT -> {
                labelDefinition.placement = LabelingPlacement.POINT_ABOVE_CENTER
                val symbol = SimpleMarkerSymbol(SimpleMarkerSymbol.Style.CIRCLE, Color.RED, 12F)
                simpleRenderer = SimpleRenderer(symbol)
            }
            GeometryType.POLYLINE -> {
                labelDefinition.placement = LabelingPlacement.LINE_ABOVE_ALONG
                val lineSymbol = SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, layer.color, layer.width.toFloat())
                simpleRenderer = SimpleRenderer(lineSymbol)
            }
            GeometryType.POLYGON -> {
                labelDefinition.placement = LabelingPlacement.AUTOMATIC
                //设置面样式
                val lineSymbol = SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, layer.borderColor, layer.width.toFloat())
                val fillSymbol = SimpleFillSymbol(SimpleFillSymbol.Style.SOLID, layer.color, lineSymbol)
                simpleRenderer = SimpleRenderer(fillSymbol)
            }
            else -> {}
        }
        featureLayer.renderer = simpleRenderer
    }

}